public class Solution {
    private int count;
    private int result;
    public int kthSmallest(TreeNode root, int k) {
        count = 0;
        result = 0;
        findHelper(root, k);
        return result;
    }
    private void findHelper(TreeNode root, int k){
        if(root == null || count > k){
            return;
        }

        findHelper(root.left, k);
        count++;
        if (count == k) {
            result = root.val;
            return;
        }
        findHelper(root.right, k);
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}